package com.yonyou.iuap.jxzy.servlet.service;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.yonyou.iuap.iweb.exception.BusinessException;
import com.yonyou.iuap.jxzy.servlet.BaedDeviceService;
import com.yonyou.iuap.jxzy.servlet.DeviceInfo;
import com.yonyou.iuap.jxzy.servlet.dao.DeviceMapper;
import com.yonyou.iuap.jxzy.utils.HttpRequestUtil;

/**
 * 刷卡控制led显示
 * @author 王格萍
 * @date 2019年6月21日
 *
 */
@Service
public class ControlGuardLEDService implements BaedDeviceService{

	public static final String LEDURL = "http://164.70.12.29:8080/unattended_hardware/hardwareOperation/showLedJX";

	private Logger logger = LoggerFactory.getLogger(this.getClass());

	@Autowired
	private DeviceMapper deviceMapper;

	@Override
	public void processor(DeviceInfo deviceinfo) throws BusinessException {
		//根据卡id获取卡号
		String cardid = (String) deviceinfo.getUserData();
		String leddeviceCode=this.deviceMapper.getDivicecode(deviceinfo.getDeviceCode());//获取设备编码  deviceinfo.getDeviceCode()：刷卡编码
		if(leddeviceCode!=null && leddeviceCode.length()>0){
			JSONObject json=new JSONObject();
			//根据卡ID获取车牌号
			String vehicle=this.deviceMapper.queryVehicleByCardid(cardid);
			if(vehicle!=null){
				json.put("deviceCode", leddeviceCode);
				json.put("ledText", vehicle);
				String result=HttpRequestUtil.sendPost2(LEDURL, json.toString());
				if("".equals(result)|| Boolean.FALSE.equals(new Boolean(result))){
					logger.error("LED显示异常");
					throw new BusinessException("LED显示异常");
				}
				//线程沉睡3秒，防止卡片未拿走
				try {
					Thread.sleep(3000);
					logger.error("led显示成功："+vehicle);
				} catch (InterruptedException e) {
					logger.error("线程异常："+e.getMessage());
					throw new BusinessException("线程异常："+e.getMessage());
				}
			}else{//根据卡id未获取到车牌号
				json.put("deviceCode", leddeviceCode);
				json.put("ledText", "车辆未发卡");
				String result=HttpRequestUtil.sendPost2(LEDURL, json.toString());
				if("".equals(result)|| Boolean.FALSE.equals(new Boolean(result))){
					logger.error("LED显示异常");
					throw new BusinessException("LED显示异常");
				}
			}
		}
	}
}
